<?php
namespace app\api\controller;

use app\BaseController;
use app\common\business\User as userBusi;
class User extends BaseController{
	// 获取登录用户信息
	public function index(){
		//echo $this->request->userId;exit;
		$user = (new userBusi())->getNormalUserById($this->request->userId);
		//halt($user);
		$userResult = [
			'id' => $user['id'],
			'username' => $user['username'],
			'sex' => $user['sex']
		];
		return show(config('status.success'),'ok',$userResult);
	}

	// 更新用户信息
	public function update(){
		$username = input('param.username','','trim');
		$sex = input('param.sex',0,'intval');
		$data = [
			'username' => $username,
			'sex' => $sex
		];
		
		// 校验数据
		$validate = (new \app\api\validate\User())->scene('update_user');
		if(!$validate->check($data)){
			return show(config('status.error'),$validate->getError());
		}
		$userId = $this->request->userId;
		$user = (new userBusi())->update($userId,$data);
	
		if(!$user){
			return show(config('status.error'),'更新失败');
		}
		return show(config('status.success'),'ok');
	}
}